package com.hk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.reggie.common.CustomException;
import com.hk.reggie.entity.Category;
import com.hk.reggie.entity.Dish;
import com.hk.reggie.entity.Setmeal;
import com.hk.reggie.mapper.CategoryMapper;
import com.hk.reggie.service.CategoryService;
import com.hk.reggie.service.DishService;
import com.hk.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteCategoryById(Long id) {
        //查询当前分类是否包含菜品，如果已经关联，则抛出异常
        int dishCount = dishService.count(new QueryWrapper<Dish>().eq("category_id", id));
        if(dishCount > 0){
            throw new CustomException("当前分类下还关联菜品，不能删除");
        }
        //查询当前分类是否包含套餐，如果已经关联，则抛出异常
        int setmealCount = setmealService.count(new QueryWrapper<Setmeal>().eq("category_id", id));
        if(setmealCount > 0){
            throw new CustomException("当前分类下还关联套餐，不能删除");
        }
        //正常删除
        this.removeById(id);
    }
}
